<?php
class News_model extends MY_Model {
	private $module = 'news';
	private $table = 'news';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title_vi` LIKE "%'.$this->input->post('content').'%" OR `title_en` LIKE "%'.$this->input->post('content').'%" OR `title_gr` LIKE "%'.$this->input->post('content').'%" OR `content_vi` LIKE "%'.$this->input->post('content').'%" OR `content_en` LIKE "%'.$this->input->post('content').'%" OR `content_gr` LIKE "%'.$this->input->post('content').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title_vi` LIKE "%'.$this->input->post('content').'%" OR `title_en` LIKE "%'.$this->input->post('content').'%" OR `title_gr` LIKE "%'.$this->input->post('content').'%" OR `content_vi` LIKE "%'.$this->input->post('content').'%" OR `content_en` LIKE "%'.$this->input->post('content').'%" OR `content_gr` LIKE "%'.$this->input->post('content').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results(PREFIX.$this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}

	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function saveManagement($fileName=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		$parent_id = $this->input->post('parentIdAdmincp');
		$current_slug = $this->input->post('slugAdmincp');
		if($this->input->post('hiddenIdAdmincp')==0){
			//Kiểm tra đã tồn tại chưa?
			$checkData = $this->checkData($this->input->post('titleVnAdmincp'));
			if($checkData){
				print 'error-title-exists';
				exit;
			}

			$checkSlug = $this->checkSlug($this->input->post('slugAdmincp'));
			if($checkSlug){
				print 'error-slug-exists';
				exit;
			}

			$data = array(
				'title_vi'=> $this->input->post('titleVnAdmincp'),
				'title_en'=> $this->input->post('titleEnAdmincp'),
				'title_gr'=> $this->input->post('titleGrAdmincp'),
				'slug'=> $this->input->post('slugAdmincp'),
				'link'=> $this->input->post('slugAdmincp'),
				'order_view'=> $this->input->post('order_viewAdmincp'),
				'parent_id' => $parent_id,
				'description_vi'=> $this->input->post('descVnAdmincp'),
				'description_en'=> $this->input->post('descEnAdmincp'),
				'description_gr'=> $this->input->post('descGrAdmincp'),
				'content_vi'=> $this->input->post('contentViAdmincp'),
				'content_en'=> $this->input->post('contentEnAdmincp'),
				'content_gr'=> $this->input->post('contentGrAdmincp'),
				'image'=> $fileName['image'],
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				$current_id = $this->db->insert_id();
				if($parent_id != 0){
					$result = $this->model->get('*', PREFIX.'category', "id = '$parent_id'");
           if (!empty($result)) {
              $this->_updateLink($current_slug, $parent_id);
           }
				}
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			//Kiểm tra đã tồn tại chưa?
			if($result[0]->title_vi!=$this->input->post('titleVnAdmincp')){
				$checkData = $this->checkData($this->input->post('titleVnAdmincp'));
				if($checkData){
					print 'error-title-exists';
					exit;
				}
			}

			if($result[0]->slug!=$this->input->post('slugAdmincp')){
				$checkSlug = $this->checkSlug($this->input->post('slugAdmincp'));
				if($checkSlug){
					print 'error-slug-exists';
					exit;
				}
			}

			//Xử lý xóa hình khi update thay đổi hình
			if($fileName['image']==''){
				$fileName['image'] = $result[0]->image;
			}else{
				@unlink(BASEFOLDER.DIR_UPLOAD_NEWS.$result[0]->image);
			}
			//End xử lý xóa hình khi update thay đổi hình

			$data = array(
				'title_vi'=> $this->input->post('titleVnAdmincp'),
				'title_en'=> $this->input->post('titleEnAdmincp'),
				'title_gr'=> $this->input->post('titleGrAdmincp'),
				'slug'=> $this->input->post('slugAdmincp'),
				'link'=> $this->input->post('slugAdmincp'),
				'order_view'=> $this->input->post('order_viewAdmincp'),
				'parent_id' => $parent_id,
				'description_vi'=> $this->input->post('descVnAdmincp'),
				'description_en'=> $this->input->post('descEnAdmincp'),
				'description_gr'=> $this->input->post('descGrAdmincp'),
				'content_vi'=> $this->input->post('contentViAdmincp'),
				'content_en'=> $this->input->post('contentEnAdmincp'),
				'content_gr'=> $this->input->post('contentGrAdmincp'),
				'image'=> $fileName['image'],
				'status'=> $status
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				//update link
				if($parent_id != 0){
					$result = $this->model->get('*', PREFIX.'category', "id = '$parent_id'");
           if (!empty($result)) {
              $this->_updateLink($current_slug, $parent_id);
           }
				}
				return true;
			}
		}
		return false;
	}

	function checkData($title){
		$this->db->select('id');
		$this->db->where('title_vi',$title);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}

	function checkSlug($slug){
		$this->db->select('id');
		$this->db->where('slug',$slug);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}

	private function _getParent($parent_id){
		$this->db->select('*');
		$this->db->where('id',$parent_id);
		$query = $this->db->get(PREFIX.'category');
		if($query->result()){
			return $query->row();
		}
		else
			return false;
	}

	private function _getSingleNews($current_slug){
		$this->db->select('*');
		$this->db->where('slug',$current_slug);
		$query = $this->db->get(PREFIX.'news');
		if($query->result()){
			return $query->row();
		}
		else
			return false;
	}

	private function _updateLink($current_slug = '', $parent_id = 0) {
		if($parent_id != 0){
			$parent = $this->_getParent($parent_id);
			$current = $this->_getSingleNews($current_slug);
			$link = $parent->link . '/' . $current_slug;
			$data['link'] = $link;
      $this->model->update(PREFIX.'news', $data, "id = $current->id");
      //pr($this->db->last_query(),1);
		}
	}

	private function _updateMenuLink($parent_id = 0, $parent_link = '') {
    $article_list = $this->model->fetch('id,parent_id,slug', PREFIX.'category', "parent_id = $parent_id", 'id', 'ASC');
    if (!empty($article_list)) {
      foreach ($article_list as $article) {
        $current_id = $article->id;
        $link = $article->slug;
        if (!empty($parent_link)) {
           $link = $parent_link . '/' . $link;
        }
        $data['link'] = $link;
        $this->model->update(PREFIX.'news', $data, "id = $current_id");
        $this->_updateMenuLink($current_id, $link);
      }
    }
	}

	function getCategory(){
		$this->db->select('*');
		$this->db->where('status',1);
		$query = $this->db->get(PREFIX.'category');
		if($query->result()){
			return $query->result();
		}
		else{
			return false;
		}
	}

	/*----------------------FRONTEND----------------------*/
	function getNews($type){
		$this->db->select('*');
		$this->db->where('type',$type);
		$this->db->where('status',1);
		$this->db->order_by('created','DESC');
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getPostByParentId($limit = 5, $startRow = 0, $id = ''){
		$this->db->select('*');
		$this->db->where('parent_id',$id);
		$this->db->where('status',1);
		$this->db->limit($limit,$startRow);
		$this->db->order_by('created','DESC');
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getTotalRow($id=''){
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->where('parent_id',$id);
		$query = $this->db->count_all_results(PREFIX.$this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}

	function getNewsDetail($slug_detail){
		$this->db->select('id,content');
		$this->db->where('slug',$slug_detail);
		$this->db->where('status',1);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getChildren($parent_id){
		$this->db->select('*');
		$this->db->where('parent_id',$parent_id);
		$this->db->where('status',1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	/*--------------------END FRONTEND--------------------*/
}
